﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Events;
using Silverlight.Weblog.Client.CoreBL.Events;
using Silverlight.Weblog.Client.DAL.Events;
using Silverlight.Weblog.Common.IoC;
using Silverlight.Weblog.Server.DAL;

namespace Silverlight.Weblog.Client.DAL.Services
{
    [Export(typeof(IInitializationService))]
    public class GoogleAnalyticsService : IInitializationService
    {
        #region Implementation of IApplicationService

        public void StartService(ApplicationServiceContext context)
        {
        }

        public void Initialize()
        {
            IEventAggregator events = IoC.Get<IEventAggregator>();
            events.GetEvent<CommentCreatedEvent>().Subscribe(CommentAdded);
            events.GetEvent<NavigateToBlogPostEvent>().Subscribe(NavigateToBlogPost);
            events.GetEvent<GoToBlogPostPermlinkEvent>().Subscribe(NavigateToBlogPostPermlinkEvent);
            events.GetEvent<NavigateToHomePageEvent>().Subscribe(NavigateToHomePage);
            events.GetEvent<DisplayCategoryEvent>().Subscribe(NavigateToCategory);
            events.GetEvent<SearchTermEvent>().Subscribe(NavigateToSearch);
            events.GetEvent<LoadBlogPostHTMLEvent>().Subscribe(LoadBlogPostHTML);
            events.GetEvent<LoadBlogPostsHTMLEvent>().Subscribe(LoadBlogPostsHTML);
        }

        public void LoadBlogPostsHTML(List<BlogPost> obj)
        {
            TrackEvent("Blog", "LoadHtmlForBlogPosts", HttpUtility.HtmlEncode(HtmlPage.Document.DocumentUri.ToString()));
        }

        public void LoadBlogPostHTML(BlogPost obj)
        {
            TrackEvent("Blog", "LoadHtmlForBlogPost",  HttpUtility.HtmlEncode(HtmlPage.Document.DocumentUri.ToString()));
        }

        public void NavigateToSearch(SearchEventData obj)
        {
            TrackEvent("Blog", "Search", obj.SearchTerm + " on " + obj.Username);
        }

        public void NavigateToCategory(Category obj)
        {
            TrackEvent("Blog", "Catrgory", obj.Name);
        }

        public void NavigateToHomePage(User obj)
        {

            TrackEvent("Blog", "HomePage", obj != null ? obj.Username : string.Empty);
        }

        public void NavigateToBlogPostPermlinkEvent(string obj)
        {
            TrackEvent("Blog", "BlogPost", obj);
        }

        public void NavigateToBlogPost(BlogPost obj)
        {
            TrackEvent("Blog", "BlogPost", obj.Permlink);
        }

        public void CommentAdded(Comment obj)
        {
            TrackEvent("Blog", "AddComent", obj.BlogPost.Permlink);
        }

        //pageTracker._trackEvent('Videos', 'Play', 'Baby\'s First Birthday')
        private void TrackEvent(string str1, string str2, string str3)
        {
            HtmlPage.Window.Eval(string.Format("pageTracker._trackEvent('{0}', '{1}', '{2}')", 
                                               JSSanitize(str1),
                                               JSSanitize(str2), 
                                               JSSanitize(str3)));
        }

        private string JSSanitize(string str)
        {
            return str.Replace(@"'", @"\'");
        }

        public void StopService()
        {
            IEventAggregator events = IoC.Get<IEventAggregator>();
            if (events != null)
            {
                events.GetEvent<CommentCreatedEvent>().Unsubscribe(CommentAdded);
                events.GetEvent<NavigateToBlogPostEvent>().Unsubscribe(NavigateToBlogPost);
                events.GetEvent<GoToBlogPostPermlinkEvent>().Unsubscribe(NavigateToBlogPostPermlinkEvent);
                events.GetEvent<NavigateToHomePageEvent>().Unsubscribe(NavigateToHomePage);
                events.GetEvent<DisplayCategoryEvent>().Unsubscribe(NavigateToCategory);
                events.GetEvent<SearchTermEvent>().Unsubscribe(NavigateToSearch);
            }
        }

        #endregion


    }
}
